/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gro;

import gro.procesos.clases.UsuarioDP;
import gro.utilitarios.Global;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.net.URL;
import javax.swing.JOptionPane;

/**
 *
 * @author Vicente
 */
public class ValidarCredenciales extends javax.swing.JFrame {

    /**
     * Creates new form ValidarCredenciales
     */
        String path = "icoMenu.png";
    URL url = this.getClass().getResource(path);
    UsuarioDP udp = new UsuarioDP();
    int usu = 0;
    public ValidarCredenciales() {
        initComponents();
            this.setIconImage(Toolkit.getDefaultToolkit().getImage(url));

    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        btnSalir = new javax.swing.JButton();
        lblUsuario = new javax.swing.JLabel();
        lblClave = new javax.swing.JLabel();
        txtClave = new javax.swing.JPasswordField();
        txtUsuario = new javax.swing.JTextField();
        btnEntrar = new javax.swing.JButton();
        lblFoto = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("GRO");
        setMinimumSize(new java.awt.Dimension(216, 325));
        setResizable(false);
        getContentPane().setLayout(null);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(204, 102, 0));
        jLabel2.setText("Gestión de Registro");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(40, 10, 160, 20);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(204, 102, 0));
        jLabel1.setText("Odontológico");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(90, 30, 110, 20);

        btnSalir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gro/imagenes/salir.png"))); // NOI18N
        getContentPane().add(btnSalir);
        btnSalir.setBounds(160, 250, 40, 30);

        lblUsuario.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblUsuario.setForeground(new java.awt.Color(204, 102, 0));
        lblUsuario.setText("Usuario:");
        getContentPane().add(lblUsuario);
        lblUsuario.setBounds(20, 170, 50, 14);

        lblClave.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblClave.setForeground(new java.awt.Color(204, 102, 0));
        lblClave.setText("Clave:");
        getContentPane().add(lblClave);
        lblClave.setBounds(20, 200, 40, 14);

        txtClave.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtClaveKeyPressed(evt);
            }
        });
        getContentPane().add(txtClave);
        txtClave.setBounds(80, 200, 120, 20);
        getContentPane().add(txtUsuario);
        txtUsuario.setBounds(80, 170, 120, 20);

        btnEntrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gro/imagenes/entrar.png"))); // NOI18N
        btnEntrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEntrarActionPerformed(evt);
            }
        });
        getContentPane().add(btnEntrar);
        btnEntrar.setBounds(110, 250, 40, 30);

        lblFoto.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gro/imagenes/fondoConLogo.png"))); // NOI18N
        lblFoto.setMaximumSize(new java.awt.Dimension(240, 340));
        lblFoto.setMinimumSize(new java.awt.Dimension(240, 340));
        lblFoto.setPreferredSize(new java.awt.Dimension(220, 340));
        getContentPane().add(lblFoto);
        lblFoto.setBounds(0, 0, 216, 306);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnEntrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEntrarActionPerformed
        FramePrincipal a = new FramePrincipal();
        a.setLocationRelativeTo(null);
        a.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnEntrarActionPerformed

    private void txtClaveKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtClaveKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            ingreso();
        }      
    }//GEN-LAST:event_txtClaveKeyPressed
public void ingreso() {
        udp.setUsuario(txtUsuario.getText());
        udp.setContrasena(String.valueOf(txtClave.getPassword()));
        if (udp.validarUsuarioDP()) {
            if (udp.getPermiso().trim().equals("Doctor")) {
                Global.per = 1;
            } else if (udp.getPermiso().trim().equals("Secretaria")) {
                Global.per = 2;
            }
            FramePrincipal a = new FramePrincipal();
            a.setLocationRelativeTo(null);
            a.setVisible(true);
            this.dispose();

        } else {
            txtClave.setText("");
            txtUsuario.setText("");
            JOptionPane.showMessageDialog(this, "El usuario o la contraseña estan incorrectas", "ERROR DE AUTENTICACIÓN", JOptionPane.ERROR_MESSAGE);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ValidarCredenciales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ValidarCredenciales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ValidarCredenciales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ValidarCredenciales.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ValidarCredenciales().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEntrar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel lblClave;
    private javax.swing.JLabel lblFoto;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JPasswordField txtClave;
    private javax.swing.JTextField txtUsuario;
    // End of variables declaration//GEN-END:variables
}
